﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Base;
using ERP.BusinessEntities;
using System.Data;
using Core;
using Utilities;

namespace MainApp.Modules.InvAdj
{
    public class InvAdjEntity: BaseEntity
    {
        SGVList<ICShipmentItemsInfo> lstICShipmentItems = new SGVList<ICShipmentItemsInfo>();
        SGVList<ICReceiptItemsInfo> lstICReceiptItems = new SGVList<ICReceiptItemsInfo>();

        public override void InitModuleObjects()
        {
            AddMainObject("ICInvAdjs", new ICInvAdjsInfo());
        }

        public override void InitLists()
        {
            base.InitLists();

            lstICShipmentItems = new SGVList<ICShipmentItemsInfo>();
            lstICShipmentItems.InitList(this, "ICInvAdjs", "ICShipmentItems", "FK_ICInvAdjs_ID");
            lstICShipmentItems.Binding("MainScreen", "ICShipmentItemsGridView");

            lstICReceiptItems = new SGVList<ICReceiptItemsInfo>();
            lstICReceiptItems.InitList(this, "ICInvAdjs", "ICReceiptItems", "FK_ICInvAdjs_ID");
            lstICReceiptItems.Binding("MainScreen", "ICReceiptItemsGridView");
        }

        public override void BeforeSaveList()
        {
            base.BeforeSaveList();

            ICInvAdjsInfo InvAdj = (ICInvAdjsInfo)MainObject;
            if (InvAdj == null) return;

            #region Shipment
            DataSet dsShipmentItems = AppContext.ShipmentItemCtrl.GetAllObjectByColumnValue("FK_ICInvAdjs_ID", InvAdj.ID);
            if (dsShipmentItems != null && dsShipmentItems.Tables.Count > 0)
            {
                foreach (DataRow drShipmentItem in dsShipmentItems.Tables[0].Rows)
                {
                    ICShipmentItemsInfo ShipmentItem = (ICShipmentItemsInfo)AppContext.ShipmentItemCtrl.GetObjectFromDataRow(drShipmentItem);
                    if (ShipmentItem != null)
                    {
                        Inventory.UpdateInventory(InventoryDirection.In, InventoryType.OnHand, ShipmentItem.FK_ICProducts_ID, ShipmentItem.FK_ICStocks_ID,
                            ShipmentItem.BatchNo, ShipmentItem.LotNo, ShipmentItem.SerialNo, ShipmentItem.Qty);
                    }
                }
            } 
            #endregion

            #region Receipt
            DataSet dsReceiptItems = AppContext.ReceiptItemCtrl.GetAllObjectByColumnValue("FK_ICInvAdjs_ID", InvAdj.ID);
            if (dsReceiptItems != null && dsReceiptItems.Tables.Count > 0)
            {
                foreach (DataRow drReceiptItem in dsReceiptItems.Tables[0].Rows)
                {
                    ICReceiptItemsInfo ReceiptItem = (ICReceiptItemsInfo)AppContext.ReceiptItemCtrl.GetObjectFromDataRow(drReceiptItem);
                    if (ReceiptItem != null)
                    {
                        Inventory.UpdateInventory(InventoryDirection.Out, InventoryType.OnHand, ReceiptItem.FK_ICProducts_ID, ReceiptItem.FK_ICStocks_ID,
                            ReceiptItem.BatchNo, ReceiptItem.LotNo, ReceiptItem.SerialNo, ReceiptItem.Qty);
                    }
                }
            }
            #endregion

        }

        public override void AfterSaveList()
        {
            base.AfterSaveList();

            ICInvAdjsInfo InvAdj = (ICInvAdjsInfo)MainObject;
            if (InvAdj == null) return;

            #region Shipment
            DataSet dsShipmentItems = AppContext.ShipmentItemCtrl.GetAllObjectByColumnValue("FK_ICInvAdjs_ID", InvAdj.ID);
            if (dsShipmentItems != null && dsShipmentItems.Tables.Count > 0)
            {
                foreach (DataRow drShipmentItem in dsShipmentItems.Tables[0].Rows)
                {
                    ICShipmentItemsInfo ShipmentItem = (ICShipmentItemsInfo)AppContext.ShipmentItemCtrl.GetObjectFromDataRow(drShipmentItem);
                    if (ShipmentItem != null)
                    {
                        Inventory.UpdateInventory(InventoryDirection.Out, InventoryType.OnHand, ShipmentItem.FK_ICProducts_ID, ShipmentItem.FK_ICStocks_ID,
                            ShipmentItem.BatchNo, ShipmentItem.LotNo, ShipmentItem.SerialNo, ShipmentItem.Qty);
                    }
                }
            }
            #endregion

            #region Receipt
            DataSet dsReceiptItems = AppContext.ReceiptItemCtrl.GetAllObjectByColumnValue("FK_ICInvAdjs_ID", InvAdj.ID);
            if (dsReceiptItems != null && dsReceiptItems.Tables.Count > 0)
            {
                foreach (DataRow drReceiptItem in dsReceiptItems.Tables[0].Rows)
                {
                    ICReceiptItemsInfo ReceiptItem = (ICReceiptItemsInfo)AppContext.ReceiptItemCtrl.GetObjectFromDataRow(drReceiptItem);
                    if (ReceiptItem != null)
                    {
                        Inventory.UpdateInventory(InventoryDirection.In, InventoryType.OnHand, ReceiptItem.FK_ICProducts_ID, ReceiptItem.FK_ICStocks_ID,
                            ReceiptItem.BatchNo, ReceiptItem.LotNo, ReceiptItem.SerialNo, ReceiptItem.Qty);
                    }
                }
            }
            #endregion

        }
    }
}
